package com.axonactive.yolo.service.internal;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import org.apache.log4j.Logger;
import org.json.JSONArray;
import org.json.JSONObject;

import com.axonactive.yolo.domain.Country;
import com.axonactive.yolo.dtotemp.WegoFlight;

public class JSONProcessor {
	private static Logger LOGGER = Logger.getLogger(JSONProcessor.class);

	public static List<Country> getCountries(String res) {
		List<Country> rs = new ArrayList<Country>();
		try {
			JSONArray countries = new JSONArray(res);
			String countryName, countryCode, currencyCode;
			JSONArray currencies=null;
			Country currency = null;
			for (int i = 0; i < countries.length(); i++) {
				currencyCode="";
				currency = new Country();
				countryName = countries.getJSONObject(i).getString("name");
				countryCode = countries.getJSONObject(i)
						.getString("alpha2Code");
				
				currencies= countries.getJSONObject(i)
						.getJSONArray("currencies");
				for (int j = 0; j < currencies.length(); j++) {
					currencyCode+="-"+currencies.getString(j);
				}
				currency.setCountryCode(countryCode);
				currency.setCountryName(countryName);
				currency.setCurrencyCode(currencyCode.substring(1));
				rs.add(currency);
			}
		} catch (Exception ex) {
			LOGGER.info("Get list currency from json exception: ", ex);
		}
		return rs;
	}

}
